package com.fw.ui.view.settings.usercontrol.merchantuser.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.ErrorBean;
import com.fw.service.merchantuser.bean.MerchantUserBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.MerchantUserBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.ConfirmDialogWindow;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.usercontrol.merchantuser.EditMerchantUserView;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditMerchantUserResetPasswordButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private MerchantUserBAO merchantUserBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	EditMerchantUserView editMerchantUserView = (EditMerchantUserView) this.getBasedComponent();
	MerchantUserBean merchantUserBean = editMerchantUserView.getMerchantUserBean();

	ApplicationUI applicationUI = (ApplicationUI) editMerchantUserView.getUI();
	sessionInfoUtils.setSessionInfoDetails(merchantUserBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	Map<String, Serializable> mapResult = this.merchantUserBAO.resetPasswordMerchantUser(merchantUserBean);
	Map<String, Object> result = new HashMap<String, Object>();
	if (mapResult != null) {
	    List<ErrorBean> errorBeans = (List<ErrorBean>) mapResult.get(List.class.getName() + "." + ErrorBean.class.getName());
	    MerchantUserBean userBean = (MerchantUserBean) mapResult.get(MerchantUserBean.class.getName());
	    if (errorBeans != null && !errorBeans.isEmpty()) {
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, errorBeans);
	    } else {
		if (userBean != null && userBean.getId() != null) {
		    editMerchantUserView.setMerchantUserBean(userBean);

		    String title = messageBAO.getMessage("message.dialog.title.info");
		    String[] params = new String[] { userBean.getPassword() };
		    String content = messageBAO.getMessageWithParams(
			    "com.fw.ui.view.settings.usercontrol.merchantuser.EditMerchantUserView.message.resetpassword", params);
		    String okCaption = messageBAO.getMessage("application.button.ok.caption");

		    ConfirmDialogWindow
			    .createConfirmDialog(editMerchantUserView.getUI(), title, content, okCaption, null, "150px", "500px");
		    return null;
		} else {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE,
			    "com.fw.ui.view.settings.usercontrol.merchantuser.EditMerchantUserView.message.resetpassword.failed");
		}
	    }
	} else {
	    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "message.error.common");
	}
	return result;

    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
